package com.fpt.edu.vn.h2tv.api.staff;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.fpt.edu.vn.h2tv.common.BookStoreUlti;
import com.fpt.edu.vn.h2tv.service.BookStoreService;
import com.fpt.edu.vn.h2tv.service.IBaseService;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;

@Path("/manageBook")
public class BookStoreAdminAPI {

    @GET
    @Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)
    public Response listBook() {
        IBaseService service = new BookStoreService();
        return buildResponse(MediaType.APPLICATION_XML, service.getResponse());
    }

    @Path("/HotBook")
    @GET
    @Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)
    public Response searchHotBook() {

        return buildResponse(MediaType.APPLICATION_XML, BookStoreService.searchHotBook());
    }

    @Path("/outOfBook")
    @GET
    @Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)
    public Response searchOutOfBook() {

        return buildResponse(MediaType.APPLICATION_XML, BookStoreService.searchOutOfBook());
    }

    @Path("/searchBook")
    @GET
    @Produces(javax.ws.rs.core.MediaType.APPLICATION_XML)
    public Response searchBook(@QueryParam(value = "txtSearchBook") String key) {

        return buildResponse(MediaType.APPLICATION_XML, BookStoreService.searchBooks(key));
    }

    @PUT
    @Produces(javax.ws.rs.core.MediaType.TEXT_HTML)
    @Consumes({MediaType.MULTIPART_FORM_DATA,MediaType.APPLICATION_FORM_URLENCODED})
    public Response updateBook(
            @Context HttpServletRequest request,
            @FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail,
            @FormDataParam(value = "txtID") int id,
            @FormDataParam(value = "txtName") String bookName,
            @FormDataParam(value = "txtCategory") Short categoryId,
            @FormDataParam(value = "txtAuthor") String author,
            @FormDataParam(value = "txtPrice") Double price,
            @FormDataParam(value = "txtDes") String des,
            @FormDataParam(value = "txtQuantity") int quantity,
            @FormDataParam("backendUrl") String backend) throws IOException {
       
        String imgUrl=null;
        if(fileDetail!=null&&fileDetail.getFileName()!=null){
            String uploadedFileLocation = request.getSession().getServletContext().getRealPath("/books");
            //using current time to sure that there're no image can the same url
            String fileName = "/"+System.currentTimeMillis()+fileDetail.getFileName();
            BookStoreUlti.writeToFile(uploadedInputStream, uploadedFileLocation+fileName);
            
             imgUrl = backend+"books"+fileName;
        }
        
        return buildResponse(MediaType.TEXT_HTML,
                BookStoreService.updateBook(id, bookName, categoryId, author, price, des, quantity, imgUrl));
    }

    @POST
    @Produces(javax.ws.rs.core.MediaType.TEXT_HTML)
    @Consumes({MediaType.MULTIPART_FORM_DATA,MediaType.APPLICATION_FORM_URLENCODED})
    public Response addBook(
            @Context HttpServletRequest request,
            @FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail,
            @FormDataParam("backendUrl") String backend,
            @FormDataParam(value = "txtName") String bookName,
            @FormDataParam(value = "txtCategory") Short categoryId, @FormDataParam(value = "txtAuthor") String author,
            @FormDataParam(value = "txtPrice") Double price, @FormDataParam(value = "txtDes") String des,
            @FormDataParam(value = "txtQuantity") int quantity) throws IOException {
        //get location of images book
        String uploadedFileLocation = request.getSession().getServletContext().getRealPath("/books");
        //using current time to sure that there're no image can the same url
        String fileName = "/"+System.currentTimeMillis()+fileDetail.getFileName();
        BookStoreUlti.writeToFile(uploadedInputStream, uploadedFileLocation+fileName);
        
        String imgUrl = backend+"books"+fileName;
        
        Timestamp CreateTime = new Timestamp(System.currentTimeMillis());
        Timestamp updateTime = null;
        Long count = (long) 0;

        return buildResponse(MediaType.TEXT_HTML, BookStoreService.addBook(bookName, categoryId, author, price, des,
                imgUrl, CreateTime, count, quantity, updateTime));
    }

    @DELETE
    @Produces(javax.ws.rs.core.MediaType.TEXT_HTML)
    public Response deleteBook(@QueryParam(value = "txtID") int id) {

        return buildResponse(MediaType.TEXT_HTML, BookStoreService.deleteBook(id));
    }

    private Response buildResponse(String mediaType, Object repsonse) {
        return Response.ok(repsonse, mediaType)
        // .header("Access-Control-Allow-Origin", "http://localhost")
        // .header("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS,TRACE,HEAD,CONNECT")
        // .header("Access-Control-Allow-Credentials",true)
        // .header("Access-Control-Allow-Headers",
        // "x-accept-charset, x-accept, origin,X-Accept-Charset,X-Accept,Content-Type,Authorization,REFERER")
                .build();

    }

    
}
